package system1

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system1"
	system1Req "github.com/flipped-aurora/gin-vue-admin/server/model/system1/request"
	"gorm.io/gorm"
)

type Sys_departmentService struct {
}

// CreateSys_department 创建部门记录
// Author [piexlmax](https://github.com/piexlmax)
func (departmentService *Sys_departmentService) CreateSys_department(department *system1.Sys_department) (err error) {
	err = global.GVA_DB.Create(department).Error
	return err
}

// DeleteSys_department 删除部门记录
// Author [piexlmax](https://github.com/piexlmax)
func (departmentService *Sys_departmentService) DeleteSys_department(ID string, userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&system1.Sys_department{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
			return err
		}
		if err = tx.Delete(&system1.Sys_department{}, "id = ?", ID).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// DeleteSys_departmentByIds 批量删除部门记录
// Author [piexlmax](https://github.com/piexlmax)
func (departmentService *Sys_departmentService) DeleteSys_departmentByIds(IDs []string, deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&system1.Sys_department{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
			return err
		}
		if err := tx.Where("id in ?", IDs).Delete(&system1.Sys_department{}).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// UpdateSys_department 更新部门记录
// Author [piexlmax](https://github.com/piexlmax)
func (departmentService *Sys_departmentService) UpdateSys_department(department system1.Sys_department) (err error) {
	err = global.GVA_DB.Save(&department).Error
	return err
}

// GetSys_department 根据ID获取部门记录
// Author [piexlmax](https://github.com/piexlmax)
func (departmentService *Sys_departmentService) GetSys_department(ID string) (department system1.Sys_department, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&department).Error
	return
}

// GetSys_department 根据ID获取部门记录
// Author [piexlmax](https://github.com/piexlmax)
func (departmentService *Sys_departmentService) GetSys_departments_with_employees(ID string) (departments []system1.Sys_department, err error) {
	err = global.GVA_DB.Preload("Employees").Where("parent_id = ?", ID).Find(&departments).Error
	return
}

// GetSys_departmentInfoList 分页获取部门记录
// Author [piexlmax](https://github.com/piexlmax)
func (departmentService *Sys_departmentService) GetSys_departmentInfoList(info system1Req.Sys_departmentSearch) (list []system1.Sys_department, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&system1.Sys_department{})
	var departments []system1.Sys_department
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Department_name != "" {
		db = db.Where("department_name LIKE ?", "%"+info.Department_name+"%")
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Order("created_at DESC,updated_at DESC").Find(&departments).Error
	return departments, total, err
}
